﻿using Authentication.Model.Model;
using Authentication.Web.Service.Core;
using Common.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Authentication.Web.Service
{
    public class AuthenticationService : ServiceBase, IAuthenticationService
    {
        public bool CheckLogin(UserRecord userRecord)
        {
            var application = Provider.Applications.RetrieveApplication(userRecord.ApplicationKey);
            if (application == null) return false;
            throw new NotImplementedException();
        }

        public UserRecord RetrieveUser(Guid applicationKey, string username)
        {
           var user = Provider.Users.RetrieveUser(applicationKey, username);
           if (user == null) return null;

           UserRecord record = new UserRecord
           {
               ApplicationKey = applicationKey,
               Password = user.Password,
               UserName = user.UserName
           };

           return record;
        }

        public UserRecord RetrieveEmail(Guid applicationKey, string email)
        {
            throw new NotImplementedException();
        }


        public bool RegisterUser(UserRecord userRecord)
        {
            var application = Provider.Applications.RetrieveApplication(userRecord.ApplicationKey);
            if (application == null) return false;

            User record = new User 
            {
                ApplicationKey = userRecord.ApplicationKey,
                Password = userRecord.Password,
                UserName = userRecord.UserName,
                Email = userRecord.Email,
                LoginFalseAttend = 0,
                IsLocked = false
            };

            return Provider.Users.AddUser(record); 
        } 
    }
}